//
//  Team9ExpertUsersVc.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/20.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertUsersVc.h"
#import "Team9Header.h"
#import "Team9ExpertHeaderView.h"
#import "JQAFNetworkTool.h"
#import "Team9ExpertUser_home_infoModel.h"
#import "Team9ExpertPro_serviceModel.h"
#import "Team9ExpertCell.h"
#import <Masonry.h>
#import "Team9DesignModel.h"
#import "Team9ExpertDetailView.h"
#import "Team9ExpertDiscussTableViewCell.h"
#import "Team9ExpertDiscussModel.h"
#import "Team9ExpertIdeabookColletionCell.h"
#import "Team9ExpertIdeabookModel.h"
#import "Team9ExpertFootView.h"
#import "Team9ExpertSectionHeaderView.h"
#import "Team9DesignVC.h"
#import "Team9renovatePushViewController.h"
#import "Team9s_discussViewController.h"
#import "Team9SaveModel.h"
#import "DataBaseTool.h"


@interface Team9ExpertUsersVc ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong) Team9BaseView *headView;
@property(nonatomic, strong) NSArray *array;

@property(nonatomic, strong) UIScrollView *darkView;
@property(nonatomic, strong) Team9ExpertDetailView *detailView;
@property(nonatomic, strong) NSArray *topicArray;
@property(nonatomic, strong) NSArray *ideabookArray;

@property(nonatomic, copy) NSString *workTotal;
@property(nonatomic, copy) NSString *topicTotal;
@property(nonatomic, copy) NSString *ideabookToal;

@property(nonatomic, strong) Team9ExpertFootView *footView;

@property(nonatomic, strong) Team9ExpertPro_serviceModel *serviceModel;
@property(nonatomic, strong) Team9ExpertUser_home_infoModel *userModel;

@property(nonatomic ,strong) Team9SaveModel *saver;
@end

@implementation Team9ExpertUsersVc

//- (void)dealloc{
//    _headView = nil;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"%@", _url);
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    
//    Team9BaseView *backView = [[Team9BaseView alloc] init];
//    backView.frame = CGRectMake(0, 0, WIDTH, 64);
//    [self.view addSubview:backView];
//    //    backView.backgroundColor = [UIColor whiteColor];
//    
//    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [backButton setTitle:@"返回" forState:UIControlStateNormal];
//    [backButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//    [backButton addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
//    backButton.frame = CGRectMake(0, 20, 60, 44);
//    [backView addSubview:backButton];
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    
    
    self.footView = [[Team9ExpertFootView alloc] init];
    self.footView.frame = CGRectMake(0, 0, WIDTH, 40 * 3 * WIDTH / 375);
    
    self.footView.Id = _Id;
    self.footView.vc = self;
    self.empty = [Team9EmptyTool toolWithScrollView:self.tableView];
    [self createData];
    
}
- (void)createHeaderView{
    
    UIView *backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, 0, WIDTH, 220 * WIDTH / 375);
    self.tableView.tableHeaderView = backView;
    if ([Team9EveningType shareEvening].eveningFailling) {
        backView.backgroundColor = [UIColor grayColor];
    } else backView.backgroundColor = [UIColor orangeColor];
    
    
    UIImageView *faceImageViw = [UIImageView new];
    [backView addSubview:faceImageViw];
    
    [faceImageViw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backView);
        make.top.equalTo(backView).offset(20);
        make.width.height.equalTo(@(100 * WIDTH / 375));
    }];
    faceImageViw.layer.cornerRadius = 50  * WIDTH / 375;
    faceImageViw.layer.masksToBounds = YES;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [backView addSubview:button];

    
    Team9BaseLabel *nickLabel = [Team9BaseLabel new];
    [backView addSubview:nickLabel];

    nickLabel.font = [UIFont systemFontOfSize:14 * WIDTH/ 375];
    nickLabel.textColor = [UIColor whiteColor];
    [nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(faceImageViw.mas_bottom).offset(10);
        make.centerX.equalTo(backView);
    }];
    
    Team9BaseLabel *locationLabel = [Team9BaseLabel new];
    locationLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    locationLabel.textColor = [UIColor whiteColor];
    [backView addSubview:locationLabel];
    
    [locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nickLabel.mas_bottom).offset(5);
        make.centerX.equalTo(backView);
    }];
    
    nickLabel.text = self.userModel.nick;
    locationLabel.text = self.serviceModel.location;
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(locationLabel.mas_bottom).offset(10);
        make.centerX.equalTo(backView);
        make.width.mas_equalTo(button.mas_height).multipliedBy(2);
    }];
    [button setTitle:@"关注" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    button.layer.borderColor = [UIColor whiteColor].CGColor;
    button.layer.borderWidth = 1.0f;
    button.layer.cornerRadius = 5.0f;
    
    [Team9Tool sd_imageView:faceImageViw image:self.userModel.face placeholderImage:@"111"];
    self.saver = [Team9SaveModel new];
    self.saver.author_id = self.Id;
    self.saver.author_nick = self.userModel.nick;
    self.saver.author_type = @"0";
    self.saver.author_face = self.userModel.face;
    DataBaseTool *dataTool = [DataBaseTool shareDataBaseTool];
    if ([dataTool isHave:self.saver] == InTable) {
        [button setTitle:@"已关注" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    } else if ([dataTool isHave:self.saver] == NotInTable){
        [button setTitle:@"关注" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    } else [button setTitle:@"错误" forState:UIControlStateNormal];
    
}
- (void)deleteData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    [[DataBaseTool shareDataBaseTool] deleteDesigner:self.saver];
    [sender setTitle:@"关注" forState:UIControlStateNormal];
    [sender addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)saveData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    [[DataBaseTool shareDataBaseTool] insertDesigner:self.saver];
    [sender setTitle:@"已关注" forState:UIControlStateNormal];
    [sender addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)setArray:(NSArray *)array
{
    if (_array != array) {
        _array = array;
        [self.tableView reloadData];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
//        NSLog(@"test1");
        Team9DesignModel *model = self.array[indexPath.row];
        Team9renovatePushViewController *vc = [Team9renovatePushViewController new];
        vc.bookId = model.modelId;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];

    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.array.count;
    }
    if (section == 1 && _topicArray.count) {
        return 1;
    }
    if (section == 2 && _ideabookArray.count) {
        return 1;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 && _array.count) {
        return 44.0f;
    }
    if (section == 1 && _topicArray.count) {
        //#warning test!
        return 44.0f;
    }
    if (section == 2 && _ideabookArray.count) {
        return 44.0f;
    }
    return 0.1f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && _array.count) {
        return 40 + 250 * WIDTH / 375;
    }
    if (indexPath.section == 1 && self.topicArray.count) {
        //#warning test!
        return (150 * self.topicArray.count + 30)* WIDTH/ 375 + 20;
    }
    if (indexPath.section == 2 && self.ideabookArray.count) {
        CGFloat n = (self.ideabookArray.count + 1) / 2;
        return WIDTH / 2 * (int)n + 30 * WIDTH / 375 + 10;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    Team9ExpertSectionHeaderView *view =[[Team9ExpertSectionHeaderView alloc] init];
    if (section == 0 && _array.count) {
        view.titleLabel.text = @"我的家";
    } else if (section == 1 && _topicArray.count) {
        view.titleLabel.text =  [NSString stringWithFormat:@"讨论 %@", self.topicTotal];
    } else if (section == 2 && _ideabookArray.count) {
        view.titleLabel.text =  [NSString stringWithFormat:@"灵感画册 %@", self.ideabookToal];
    }
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        static NSString *reuse = @"expertCellReuse";
        Team9ExpertCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9ExpertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        Team9DesignModel *model = self.array[indexPath.row];
        cell.model = model;
        return cell;
    }
    if (indexPath.section == 1) {
        static NSString *reuse = @"DiscussTableViewCellReuse";
        Team9ExpertDiscussTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9ExpertDiscussTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        cell.Id = _Id;
        cell.vc = self;
        __weak __typeof(self)weakSelf = self;
        [cell setBlock:^(NSInteger index) {
            Team9ExpertDiscussModel *model = weakSelf.topicArray[index];
            Team9s_discussViewController *vc = [Team9s_discussViewController new];
            vc.topicID = model.modelId;
            weakSelf.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        cell.array = self.topicArray;
        return cell;
    }
    if (indexPath.section == 2) {
        static NSString *reuse = @"IdeabookColletionCellReuse";
        Team9ExpertIdeabookColletionCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9ExpertIdeabookColletionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        cell.Id = _Id;
        cell.vc = self;
        __weak __typeof(self)weakSelf = self;
        [cell setBlock:^(NSInteger index) {
            weakSelf.navigationController.navigationBar.hidden = NO;
            Team9ExpertIdeabookModel *model = weakSelf.ideabookArray[index];
            Team9DesignVC *vc = [Team9DesignVC new];
            vc.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%@&tab=photo", model.col_id];
            weakSelf.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        cell.array = self.ideabookArray;
        return cell;
    }
    return nil;
    
}
- (void)createData{
    //    NSLog(@"%@", _url);
    [Team9RefreshTool getUrl:_url success:^(id result) {
        self.tableView.tableFooterView = _footView;
        
        self.workTotal = result[@"data"][@"user_home_work_list"][@"total"];
        self.topicTotal = result[@"data"][@"user_home_topic_list"][@"topic_total"];
        self.ideabookToal = result[@"data"][@"user_home_ideabook_list"][@"total"];
        
        self.ideabookArray = [Team9ExpertIdeabookModel baseModelByArr:result[@"data"][@"user_home_ideabook_list"][@"collections"]];
        self.topicArray = [Team9ExpertDiscussModel baseModelByArr:result[@"data"][@"user_home_topic_list"][@"topic_list"]];
        
        self.serviceModel = [Team9ExpertPro_serviceModel new];
        self.userModel = [Team9ExpertUser_home_infoModel new];
        [self.serviceModel setValuesForKeysWithDictionary: result[@"data"][@"user_home_info"][@"pro_service"]];
        [self.userModel setValuesForKeysWithDictionary: result[@"data"][@"user_home_info"][@"user_info"]];
        
        [self createHeaderView];
        
        self.detailView.string = self.userModel.nick;
        self.detailView.model = self.serviceModel;
        self.footView.model = self.userModel;
        self.array = @[];
        if (![result[@"data"][@"user_home_showhome"] isKindOfClass:[NSArray class]]) {
            Team9DesignModel *model = [Team9DesignModel new];
            [model setValuesForKeysWithDictionary:result[@"data"][@"user_home_showhome"][@"showhome"]];
            self.array = [NSArray arrayWithObject:model];
        }
        [self.darkView setContentOffset:CGPointZero];
    } scrollView:self.tableView empty:self.empty];
}
- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController.navigationBar setHidden:NO];
}


@end
